package gat.api.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class RedissionConfig {

    @Value("${spring.redis.host:}")
    private String redisHost;
    @Value("${spring.redis.password}")
    private String redisPassword;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.profiles.active}")
    private String active;

    // redission通过redissonClient对象使用 // 如果是多个redis集群，可以配置
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson(){
        Config config = new Config();
        // 创建单例模式的配置
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://" + redisHost + ":" + port).setPassword(redisPassword);
        // 本地开发 连接池小一点
        if ("dev".equals(active)) {
            singleServerConfig.setConnectionPoolSize(10)
                    .setConnectionMinimumIdleSize(5);
        }
        return Redisson.create(config);
    }


}
